package com.pan.note.controller;

import com.pan.note.service.NoteService;
import com.pan.note.util.JwtUtil;
import com.pan.note.vo.ImgResponseData;
import com.pan.note.vo.ChatMsgVo;
import com.pan.note.vo.NotePullVo;
import com.pan.note.vo.NoteUploadVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

/**
 * 秀恩爱说说相关controller
 */
@Controller
@RequestMapping(value = "/note")
public class NoteController {

    @Autowired
    private NoteService noteService;

    /**
     * 保存说说图片
     * @param file 说说图片
     * @return 保存结果
     */
    @ResponseBody
    @RequestMapping(value = "/image/save",method = RequestMethod.POST)
    public ResponseEntity<ImgResponseData> saveImg(MultipartFile file){
        return ResponseEntity.ok(noteService.saveImg(file));
    }

    /**
     * 保存说说主体
     * @param noteUploadVo 前端返回的说说实体类
     * @return 保存结果
     */
    @ResponseBody
    @RequestMapping(value = "/save",method = RequestMethod.POST)
    public ResponseEntity<String> saveMsg(@RequestBody NoteUploadVo noteUploadVo){
        return ResponseEntity.ok(noteService.saveNote(noteUploadVo));
    }

    /**
     * 拉取说说主体
     * @return 保存结果
     */
    @ResponseBody
    @RequestMapping(value = "/pull",method = RequestMethod.GET)
    public ResponseEntity<List<NotePullVo>> pullMsg(){
        List<NotePullVo> notePullVos = noteService.pullNote();
        return ResponseEntity.ok(notePullVos);
    }

    /**
     * 拉取聊天记录
     */
    @ResponseBody
    @RequestMapping(value = "/chat/msg",method = RequestMethod.GET)
    public ResponseEntity<List<ChatMsgVo>> pullChatMsg(String id, HttpServletRequest request){
        //登录成功后，获取用户token
        String token = request.getHeader("token");
        String accountId = JwtUtil.getUserNameByToken(token);
        List<ChatMsgVo> chatMsgVos = noteService.pullChatMsg(accountId, id);
        return ResponseEntity.ok(chatMsgVos);
    }

}
